package v1

import (
	"usplatformserver/internal/service"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/errcode"

	"github.com/gin-gonic/gin"
)

type Patient struct{}

func NewPatient() Patient {
	return Patient{}
}

// @Summer 获取诊断列表
// @Produce json
// @param token header string true "token"
// @param organType query int true "检查类型"
// @Param option query string false "搜索条件" maxlength(100)
// @Param beginTime query string false "起始时间"
// @param endTime query string false "截止时间"
// @Param page query int false "页码"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} model.PatientListSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/patients [get]
func (p Patient) List(c *gin.Context) {
	param := service.PatientListRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	svc := service.New(c.Request.Context())
	pager := app.Pager{
		Page:     app.GetPage(c),
		PageSize: app.GetPageSize(c),
	}

	totalRows, err := svc.CountPatient(&param)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetUserCountFail)
		return
	}

	users, err := svc.GetPatientList(&param, &pager)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetUserListFail)
		return
	}
	response.ToResponseList(users, totalRows)
}

// @Summer 获取诊断列表
// @Produce json
// @param fileId query int true "fileId"
// @Success 200 {object} model.PatientInfoSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/patient/fileid [get]
func (p Patient) GetPatientInfoByFileId(c *gin.Context) {
	param := service.GetPatientInfoByFileIdRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	svc := service.New(c.Request.Context())

	patientInfo, err := svc.GetPatientInfoByFileId(*param.FileId)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetPatientByFileIdFail)
		return
	}

	response.ToResponse(patientInfo)
}
